<?php 

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use app\admin\controller\Admin;

class Engine extends Admin
{
    /**
     * 主页列表展示
     * @return [type] [description]
     */
    public function index()
    {
        $post = input('post.');
        if (!empty($post['search'])) {
            $list = db('project_application')->where(array('project_name'=>array('like', '%'.$post['search'].'%')))->paginate(6); 
        } else {
            $list = db('project_application')->paginate(6);
        }
        return view('admin@engine/index', [
            'list' => $list
        ]);
    }


    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        // echo 'read'; die;
        // 是否为Ajax请求
        if (!Request::instance()->isAjax()) {
            $this->error('你是不是迷路了?', '/');
        }

        // 查询指定用户信息 显示查询
        $row = Db::name('project_application')->field(true)->find($id);

        if ($row === null) {
            $info['status'] = false;
            $info['title'] = '查无数据';
            $info['datas'] = '查无数据,请您重试!';
        } else {
            $info['status'] = true;
            $info['title'] = $row['project_name'];
            $info['data'] = $row['construction_content'];
            $info['id'] = $row['id'];
            $info['contract'] = $row['project_contract_name'];
        }
        return json($info);
    }



    /**
     * 渲染增加页面
     */
    public function create()
    {
        return view('admin@engine/add');
    }

    /**
     * 执行添加
     */
    public function save(Request $request)
    {
        $data = $request->post();
        $data['project_name'] = htmlentities($data['project_name']);
        $data['project_start_name'] = htmlentities($data['project_start_name']);
        $data['project_price'] = htmlentities($data['project_price']);
        $data['project_address'] = htmlentities($data['project_address']);
        $data['construction_content'] = htmlentities($data['construction_content']);
        $data['build_unit'] = htmlentities($data['build_unit']);
        $data['construction_unit'] = htmlentities($data['construction_unit']);
        $data['project_contract_name'] = htmlentities($data['project_contract_name']);

        // var_dump($data); exit;
        $project_name = db('project_application')->field('project_name')->select();
        // var_dump($project_name);
        foreach ($project_name as $k => $v) {
            if( in_array($data['project_name'],$v) ){
                return $this->error('请重新填写项目名称!');
            }
        }
        $file = request()->file('project_contract_file');
        // var_dump($file); exit;
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads\2018');

            // /uploads/20180929\5cac6e62ccfa7549a93ef848152aced3.txt
            // var_dump($info);
            if($info){
                // 成功上传后 获取上传信息
                // 输出 txt
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.txt
                // echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.txt
                // echo $info->getFilename();
                 

                $filearr = ['doc', 'txt'];
                $data['project_contract_file'] = '/uploads/'.$info->getSaveName();
                if(!in_array(pathinfo($data['project_contract_file'], PATHINFO_EXTENSION),$filearr)){
                     return $this->error('文件格式不正确,请上传doc,txt文件');
                  // echo "文件格式不正确,请上传doc,txt文件";
                  // die;
                }
                // var_dump(pathinfo($data['project_contract_file']));
                // var_dump($data['project_contract_file']); exit;
                $res =explode('\\', $data['project_contract_file']);
                $filesa=implode('/',$res);
                $data['project_contract_file'] = $filesa;
            } else {
                 // 上传失败获取错误信息
                echo $file->getError();
            }
        } else {
            $data['project_contract_file'] = '';
            return $this->error('请上传您的文件');
        }
        // var_dump($data); exit;
        // var_dump($file); exit;

        $result = db('project_application')->insert($data);
        if ($result > 0) {
            return $this->success('添加成功!', url('admin/engine/index'));
        } else {
            return $this->error('添加失败!');
        }
    }

    /**
     * 渲染编辑页面
     */
    public function edit($id)
    {
        $list = db('project_application')->find($id);
        // var_dump($list); exit;
        return view('admin@engine/edit', [
            'list' => $list
        ]);
    }

    /**
     * 更新项目
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function update(Request $request, $id)
    {
        $info = $request->put();
        // var_dump($info); exit; 
        $id = $info['id'];
        $data = [
            'project_name'          => htmlentities($info['project_name']),
            'project_price'         => htmlentities($info['project_price']),
            'project_address'       => htmlentities($info['project_address']),
            'start_time'            => htmlentities($info['start_time']),
            'makespan'              => htmlentities($info['makespan']),
            'construction_content'  => htmlentities($info['construction_content']),
            'build_unit'            => htmlentities($info['build_unit']),
            'construction_unit'     => htmlentities($info['construction_unit']),
            'status'                => htmlentities($info['status'])
        ];
        $result = db('project_application')->where('id', $id)->update($data);
        // var_dump($result); die;
        if ($result > 0) {
            return $this->success('更新成功!', url('admin/engine/index'));
        } else {
            return $this->error('更新失败!');
        }
    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {   
        $result = db('project_application')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为'.$id.'的项目删除成功!';
        }else{
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = '删除失败';
        }


        // echo json_encode($result);
        return json($info);
    }

    /**
     * 通过审核
     * @param  [type] $id int 传过来的数据id
     */
    public function onPass($id)
    {
        $id = $id;
        // var_dump($id); die;
        $date = date('Y-m-d', time());
        $data = [
            'status'=>'2',
            'start_time' => $date
        ];
        $result = db('project_application')->where('id', $id)->update($data);
        if ($result > 0) {
            return $this->redirect(url('admin/engine/index'));
        } else {
            return $this->error('更新失败!');
        }
    }



    /**
     * 确认完工
     * @param  [type] $id int 传过来的数据id
     */
    public function endPass($id)
    {
        $id = $id;
        $date= date('Y-m-d', time());
        // var_dump($date);
        // var_dump($id); die;
        $data = [
            'status'=>'3',
            'makespan' => $date
        ];
        $result = db('project_application')->where('id', $id)->update($data);
        // echo db('project_application')->getlastsql();
        // var_dump($result); die;
        if ($result > 0) {
            return $this->redirect(url('admin/engine/index'));
        } else {
            return $this->error('更新失败!');
        }
    }



    public function _empty(Request $request)
    {
        return view('/error');
    }




}








